<template>
  <view class="transactions-container">
    <view class="header-section">
      <text class="page-title">交易记录</text>
    </view>
    
    <view class="filter-section">
      <view class="date-filter">
        <picker mode="date" :value="startDate" @change="onStartDateChange">
          <view class="picker">
            {{ startDate }}
          </view>
        </picker>
        <text class="divider">至</text>
        <picker mode="date" :value="endDate" @change="onEndDateChange">
          <view class="picker">
            {{ endDate }}
          </view>
        </picker>
      </view>
      <button class="filter-button" @click="filterTransactions">筛选</button>
    </view>
    
    <view class="transactions-list">
      <view class="transaction-item" v-for="transaction in filteredTransactions" :key="transaction.id">
        <view class="transaction-info">
          <text class="transaction-type" :class="getTransactionTypeClass(transaction.type)">
            {{ transaction.typeText }}
          </text>
          <text class="transaction-time">{{ transaction.time }}</text>
        </view>
        <view class="transaction-amount" :class="getAmountClass(transaction.amount)">
          {{ transaction.amount > 0 ? '+' : '' }}{{ transaction.amount }}
        </view>
      </view>
    </view>
    
    <view class="empty-state" v-if="filteredTransactions.length === 0">
      <text class="empty-text">暂无交易记录</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { cardApi } from '../../src/services/api'

// 筛选日期
const startDate = ref('2025-10-01')
const endDate = ref('2025-10-31')

// 交易记录
const transactions = ref([
  {
    id: 1,
    type: 'recharge',
    typeText: '充值',
    amount: 1000,
    time: '2025-10-25 10:30'
  },
  {
    id: 2,
    type: 'consume',
    typeText: '消费',
    amount: -50,
    time: '2025-10-24 14:20'
  },
  {
    id: 3,
    type: 'recharge',
    typeText: '充值',
    amount: 500,
    time: '2025-10-20 09:15'
  },
  {
    id: 4,
    type: 'consume',
    typeText: '消费',
    amount: -80,
    time: '2025-10-18 16:45'
  },
  {
    id: 5,
    type: 'bonus',
    typeText: '奖励',
    amount: 100,
    time: '2025-10-15 11:20'
  }
])

// 筛选后的交易记录
const filteredTransactions = ref([...transactions.value])

// 页面加载时获取交易记录
onMounted(() => {
  fetchTransactions()
})

// 获取交易记录
const fetchTransactions = async () => {
  try {
    // 调用获取交易记录的API
    const response: any = await cardApi.getTransactions()
    transactions.value = response.data
    filteredTransactions.value = [...response.data]
    console.log('获取交易记录成功')
  } catch (error) {
    console.error('获取交易记录失败:', error)
    uni.showToast({
      title: '获取交易记录失败',
      icon: 'none'
    })
  }
}

// 获取交易类型样式类
const getTransactionTypeClass = (type: string) => {
  switch (type) {
    case 'recharge': return 'type-recharge'
    case 'consume': return 'type-consume'
    case 'bonus': return 'type-bonus'
    default: return ''
  }
}

// 获取金额样式类
const getAmountClass = (amount: number) => {
  return amount > 0 ? 'amount-positive' : 'amount-negative'
}

// 开始日期变化
const onStartDateChange = (e: any) => {
  startDate.value = e.detail.value
}

// 结束日期变化
const onEndDateChange = (e: any) => {
  endDate.value = e.detail.value
}

// 筛选交易记录
const filterTransactions = () => {
  // 这里应该根据日期筛选记录
  // 现在只是演示，直接使用所有记录
  filteredTransactions.value = [...transactions.value]
  uni.showToast({
    title: '筛选成功',
    icon: 'success'
  })
}
</script>

<style lang="scss" scoped>
.transactions-container {
  min-height: 100vh;
  background-color: #FFFFFF;
  padding: 20rpx;
}

.header-section {
  background-color: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  
  .page-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
    text-align: center;
  }
}

.filter-section {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.date-filter {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.picker {
  background-color: #f5f5f5;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
}

.divider {
  font-size: 28rpx;
  color: #666;
}

.filter-button {
  background-color: #4B0082;
  color: #fff;
  border: none;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
}

.transactions-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.transaction-item {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.transaction-info {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

.transaction-type {
  font-size: 32rpx;
  font-weight: bold;
}

.type-recharge {
  color: #00c853;
}

.type-consume {
  color: #ff3b30;
}

.type-bonus {
  color: #ff9500;
}

.transaction-time {
  font-size: 24rpx;
  color: #999;
}

.transaction-amount {
  font-size: 32rpx;
  font-weight: bold;
}

.amount-positive {
  color: #00c853;
}

.amount-negative {
  color: #ff3b30;
}

.empty-state {
  text-align: center;
  padding: 100rpx 0;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}
</style>